package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pojo.Vip;
import com.pojo.Vipaccount;
import com.service.VipService;
import com.util.LayuiTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Transactional
@RequestMapping("/vip")
public class VipController {
    @Autowired
    public VipService vipService;

//    查询vip所有
    @RequestMapping("/list")
    @ResponseBody
    public String vipList(Integer vid,  String vname,  String grade){
        return JSON.toJSONString(LayuiTable.tableData(vipService.vipFindLike(vid, vname, grade).size(),
                vipService.vipFindLike(vid, vname, grade)),
                SerializerFeature.DisableCircularReferenceDetect);
    }




    //    会员注册
    @RequestMapping("/add")
    @ResponseBody
    public String vipAdd(String viplist){
        List<Vip> vips=vipService.vipFindAll();
        Integer vid=vips.get(vips.size()-1).getVipaccounts().getVid()+1;
        Map add= JSON.parseObject(viplist);
        String vname= String.valueOf(add.get("vname"));
        String sex= String.valueOf(add.get("sex"));
        String idcard= String.valueOf(add.get("idcard"));
        String phone= String.valueOf(add.get("phone"));
        Vip vip=new Vip(vid,vname,sex.charAt(0),idcard,phone,null);
        int a=vipService.vipInsert(vip);
        int b=vipService.vipaccountInsert(new Vipaccount(vid,String.valueOf(vid),0,null));
        if (a+b==2){
            return "turn";
        }else {
            return "false";
        }
    }

    //    会员编辑
    @RequestMapping("/edit")
    @ResponseBody
    public String vipedit(String viplist){
        Map edit= JSON.parseObject(viplist);
        Integer vid= Integer.parseInt(edit.get("vid").toString());
        String vname= String.valueOf(edit.get("vname"));
        String sex= String.valueOf(edit.get("sex"));
        String idcard= String.valueOf(edit.get("idcard"));
        String phone= String.valueOf(edit.get("phone"));
        int a=vipService.vipupdate(new Vip(vid,vname,sex.charAt(0),idcard,phone,null));
        if (a==0){
            return "false";
        }else {
            return "true";
        }
    }

    //    员工离职
    @RequestMapping("/del")
    @ResponseBody
    public String vipdel(Integer vid){
        int a=vipService.vipaccountDelete(vid);
        int b=vipService.vipDelete(vid);
        if (a+b==2){
            return "turn";
        }else {
            return "false";
        }
    }


}
